import grace from "../../utils/grace.js"
grace.page({
      data: {
            order_id: 0,
            order_amount: 0,
            pay_method: 'wx',
            user: {}
      },
      onLoad: function(options) {
            let param = options[0][0];
            if (param) {
                  this.$data.order_id = param['order_id'];
                  this.$data.order_amount = param['order_amount'];
            }
            this.loadData();
      },
      loadData: function() {
            this.$http.get('api/user/info', {}).then((res) => {
                  this.$data.user = res;
            })
      },
      submitPay: function(e) {
            if (this.$data.pay_method == 'wx') {
                  this.wxPay();
            } else {
                  this.balancePay();
            }
      },
      balancePay: function() {
            let order_id = this.$data.order_id;
            wx.showToast({
                  title: '提交中..',
                  icon: 'loading',
                  duration: 2000
            })
            this.$http.get('/api/order/user_balance_pay', {
                  order_id: order_id
            }).then((res) => {
                  this.toast('支付成功');
                  if (res.is_pindan == 1) {
                        wx.redirectTo({
                              url: '../spell_pay_success/spell_pay_success',
                        });
                  } else {
                        wx.redirectTo({
                              url: '../my_order/my_order',
                        });
                  }

            });
      },

      wxPay: function() {
            let that = this;
            let order_id = this.$data.order_id;
            wx.showToast({
                  title: '请等待..',
                  icon: 'loading',
                  duration: 2000
            })
            this.$http.get('api/order/user_order_pay', {
                  order_id: order_id,
                  pay_type: 2,
            }).then((res) => {
                  let data = JSON.parse(res.data);
                  wx.requestPayment({
                        'timeStamp': data.timeStamp,
                        'nonceStr': data.nonceStr,
                        'package': data.package,
                        'signType': data.signType,
                        'paySign': data.paySign,
                        'success': function(res) {
                              that.toast('支付成功');
                              wx.redirectTo({
                                    url: '../my_order/my_order',
                              });
                        },
                        'fail': function(res) {
                              if (res.errMsg != 'requestPayment:fail cancel') {
                                    that.toast('支付失败');
                              }
                              console.log(res);
                        }
                  })
            });
      },
      pay_method_change: function(e) {
            this.$data.pay_method = e.detail.value;
      },


})